package com.yvon.maple.cloud.web.filter;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.yvon.maple.pojo.LoginUser;
import lombok.experimental.UtilityClass;

/**
 * @author : Yvon
 * @since : 2021-08-26
 */
@UtilityClass
public class LoginUserUtil {

    private final ThreadLocal<LoginUser> THREAD_LOCAL_USER = new TransmittableThreadLocal<>();


    public void setUser(LoginUser loginUser) {
        THREAD_LOCAL_USER.set(loginUser);
    }

    public Long getUserId() {
        return THREAD_LOCAL_USER.get().getId();
    }

    public String getUsername() {
        return THREAD_LOCAL_USER.get().getUsername();
    }

    public Boolean isAdmin() {
        return THREAD_LOCAL_USER.get().getIsAdmin();
    }

    public LoginUser getUserInfo() {
        return THREAD_LOCAL_USER.get();
    }

    public Boolean isLogin() {
        return THREAD_LOCAL_USER.get().getIsAdmin();
    }

    public void remove() {
        THREAD_LOCAL_USER.remove();
    }
}
